package com.rrd.itext.utils;

import com.itextpdf.text.*;
import com.itextpdf.text.log.Logger;
import com.itextpdf.text.log.LoggerFactory;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.*;
import com.itextpdf.tool.xml.exceptions.LocaleMessages;
import com.itextpdf.tool.xml.pipeline.AbstractPipeline;
import com.itextpdf.tool.xml.pipeline.WritableElement;
import com.itextpdf.tool.xml.pipeline.ctx.MapContext;
import java.util.Iterator;
import java.util.List;

public class CPdfWriterPipeline extends AbstractPipeline {
    
    public CPdfWriterPipeline() {
        super(null);
    }
    
    public CPdfWriterPipeline(Pipeline next) {
        super(next);
    }
    
    public CPdfWriterPipeline(Document doc, PdfWriter writer, java.util.List<Element> list) {
        super(null);
        this.elements = list;
        this.doc = doc;
        this.writer = writer;
        continiously = Boolean.valueOf(true);
    }
    
    public Pipeline init(WorkerContext context) throws PipelineException {
        MapContext mc = new MapContext();
        continiously = Boolean.TRUE;
        mc.put("CONTINUOUS", continiously);
        if (null != doc)
            mc.put("DOCUMENT", doc);
        if (null != writer)
            mc.put("WRITER", writer);
        context.put(getContextKey(), mc);
        return super.init(context);
    }
    
    private void write(WorkerContext context, ProcessObject po) throws PipelineException {
        label0: {
            MapContext mp = (MapContext) getLocalContext(context);
            if (!po.containsWritable())
                break label0;
            Document doc = (Document) mp.get("DOCUMENT");
            boolean continuousWrite = ((Boolean) mp.get("CONTINUOUS")).booleanValue();
            com.itextpdf.tool.xml.Writable writable = null;
            do {
                do
                    if (null == (writable = po.poll()))
                        break label0;
                while (!(writable instanceof WritableElement));
                Iterator i$ = ((WritableElement) writable).elements().iterator();
                do {
                    if (!i$.hasNext()){
                        break;
                    }
                    Element e = (Element) i$.next();
                    this.elements.add(e);
                    try {
                        if (!doc.add(e))
                            LOG.trace(String.format(LocaleMessages.getInstance()
                                    .getMessage("pipeline.pdfwriter.elemnotadded"),
                                    new Object[] { e.toString() }));
                    } catch (DocumentException e1) {
                        if (!continuousWrite)
                            throw new PipelineException(e1);
                        LOG.error(LocaleMessages.getInstance().getMessage("pipeline.pdfwriter.elemnotaddedexc"), e1);
                    }
                } while (true);
            } while (true);
        }
    }
    
    public Pipeline open(WorkerContext context, Tag t, ProcessObject po) throws PipelineException {
        write(context, po);
        return getNext();
    }
    
    public Pipeline content(WorkerContext context, Tag currentTag, String text, ProcessObject po)
            throws PipelineException {
        write(context, po);
        return getNext();
    }
    
    public Pipeline close(WorkerContext context, Tag t, ProcessObject po) throws PipelineException {
        write(context, po);
        return getNext();
    }
    
    public void setDocument(Document document) {
        doc = document;
    }
    
    public void setWriter(PdfWriter writer) {
        this.writer = writer;
    }
    
    private static final Logger LOG = LoggerFactory.getLogger("com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline");
    
    private Document doc;
    
    private PdfWriter writer;
    
    public static final String DOCUMENT = "DOCUMENT";
    
    public static final String WRITER = "WRITER";
    
    public static final String CONTINUOUS = "CONTINUOUS";
    
    private Boolean continiously;
    
    private java.util.List<Element> elements;
    
}
